*** we need to get the variance of daily returns for each bank 
*** then we need to get variance of 5 day, 10 day, 20 day, and 50 day units for each bank 
*** and we literally look at the ratio of these variances 

***questions
*** to get the 5-day returns, do you just add up five subsequent days of returns? 

*** let's start by opening the data file we want and converting the dates properly 

use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns.dta", clear

gen date_num = date(date, "MDY")
drop date
rename date_num date 

*** turn it into a variable we can use for time series 
egen date_num = group(date)
egen id = group(permno)
xtset id date_num

gen lag_price_1=prc[_n-1]
gen lag_price_5=prc[_n-5]
gen lag_price_10=prc[_n-10]
gen lag_price_20=prc[_n-20]
gen lag_price_50=prc[_n-50]

gen return_1=(prc-lag_price_1)/lag_price_1
gen return_5=(prc-lag_price_5)/lag_price_5
gen return_10=(prc-lag_price_10)/lag_price_10
gen return_20=(prc-lag_price_20)/lag_price_20
gen return_50=(prc-lag_price_50)/lag_price_50

*** let's get log returns also for a sanity check 
gen log_return_1=log(prc)-log(lag_price_1)
gen log_return_5=log(prc)-log(lag_price_5)
gen log_return_10=log(prc)-log(lag_price_10)
gen log_return_20=log(prc)-log(lag_price_20)
gen log_return_50=log(prc)-log(lag_price_50)

gen year = year(date) 
gen crisis = . 
replace crisis = 0 if (year >= 2002 & year <= 2007)
replace crisis = 1 if (year>=2010 & year <= 2015) 
drop if crisis == . 


save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns_2.dta", replace

*** you will have to keep only certain days in order to get the non-overlapping intervals you want. you will keep them only if i.e. date_num divisible + 1 is divisible by 10 

*** also note that we want to compute these var ratios separately pre and post-crisis 

*** let's try and get the right variances stored for starting with just the daily returns 
use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns_2.dta", clear
by id crisis, sort: sum(return_1)
egen mean_returns=mean(return_1), by(id crisis)
egen sd_returns=sd(return_1), by(id crisis)
gen var_returns=sd_returns*sd_returns

egen mean_log_returns=mean(log_return_1), by(id crisis)
egen sd_log_returns=sd(log_return_1), by(id crisis)
gen var_log_returns=sd_log_returns*sd_log_returns

by id crisis, sort: gen output_indicator = _n 
br id ticker permno crisis ret var_returns var_log_returns if output_indicator == 1 

rename var_returns daily_var 
rename var_log_returns daily_log_var 
keep date date_num id comnam ticker permno crisis daily_var daily_log_var
save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/daily_returns.dta", replace 

**** now we want five day windows
**SANITY, check observation number in var calculations 
use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns_2.dta", clear
keep if mod(date_num,5)==1

by id crisis, sort: sum(return_5)
egen mean_returns=mean(return_5), by(id crisis)
egen sd_returns=sd(return_5), by(id crisis)
gen var_returns=sd_returns*sd_returns

egen mean_log_returns=mean(log_return_5), by(id crisis)
egen sd_log_returns=sd(log_return_5), by(id crisis)
gen var_log_returns=sd_log_returns*sd_log_returns

by id crisis, sort: gen output_indicator = _n 
br id ticker permno crisis ret var_returns var_log_returns if output_indicator == 1 

rename var_returns weekly_var 
rename var_log_returns  weekly_log_var 
keep date date_num id ticker permno crisis weekly_var weekly_log_var
save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/weekly_returns.dta", replace 

****** now we want ten day windows 
use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns_2.dta", clear
keep if mod(date_num,10)==1

by id crisis, sort: sum(return_10)
egen mean_returns=mean(return_10), by(id crisis)
egen sd_returns=sd(return_10), by(id crisis)
gen var_returns=sd_returns*sd_returns

egen mean_log_returns=mean(log_return_10), by(id crisis)
egen sd_log_returns=sd(log_return_10), by(id crisis)
gen var_log_returns=sd_log_returns*sd_log_returns

by id crisis, sort: gen output_indicator = _n 
br id ticker permno crisis ret var_returns var_log_returns if output_indicator == 1 

rename var_returns ten_var 
rename var_log_returns  ten_log_var 
keep date date_num id ticker permno crisis ten_var ten_log_var
save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/ten_returns.dta", replace 


**** now we want twenty day windows 
use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns_2.dta", clear
keep if mod(date_num,20)==1

by id crisis, sort: sum(return_20)
egen mean_returns=mean(return_20), by(id crisis)
egen sd_returns=sd(return_20), by(id crisis)
gen var_returns=sd_returns*sd_returns

egen mean_log_returns=mean(log_return_20), by(id crisis)
egen sd_log_returns=sd(log_return_20), by(id crisis)
gen var_log_returns=sd_log_returns*sd_log_returns

by id crisis, sort: gen output_indicator = _n 
br id ticker permno crisis ret var_returns var_log_returns if output_indicator == 1 

rename var_returns twen_var 
rename var_log_returns  twen_log_var 
keep date date_num id ticker permno crisis twen_var twen_log_var
save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/twen_returns.dta", replace 


*** now we want fifty day windows 
use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/returns_2.dta", clear
keep if mod(date_num,50)==1

by id crisis, sort: sum(return_50)
egen mean_returns=mean(return_50), by(id crisis)
egen sd_returns=sd(return_50), by(id crisis)
gen var_returns=sd_returns*sd_returns

egen mean_log_returns=mean(log_return_50), by(id crisis)
egen sd_log_returns=sd(log_return_50), by(id crisis)
gen var_log_returns=sd_log_returns*sd_log_returns

by id crisis, sort: gen output_indicator = _n 
br id ticker permno crisis ret var_returns var_log_returns if output_indicator == 1 

rename var_returns fif_var 
rename var_log_returns  fif_log_var 
keep date date_num id ticker permno crisis fif_var fif_log_var
save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/fif_returns.dta", replace 

****
*** MERGE the different vars 
 merge 1:1 date_num id using "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/daily_returns.dta"
keep if _merge == 3
drop _merge 
 
merge 1:1 date_num id using "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/weekly_returns.dta"
keep if _merge == 3
drop _merge 

merge 1:1 date_num id using "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/twen_returns.dta"
keep if _merge == 3
drop _merge 

merge 1:1 date_num id using "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/ten_returns.dta"
keep if _merge == 3
drop _merge 

save "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/full_returns.dta", replace 

use "/Users/natashasarin/Dropbox/Z__Natamir/Natasha_Research/Banks/Autocorelation/Data/VR/full_returns.dta", clear

by id crisis, sort: gen output_indicator = _n 
br if output_indicator == 1 

*** let's try just for baseline presentation to only do those big6 
gen big6 = 0 
replace big6 = 1 if (permno == 38703 | permno==47896 | permno==59408 | permno==69032 |permno==70519 | permno == 86868)

*** only want to keep one pre and one post-crisis observation per bank 
drop if output_indicator != 1

** now we can do the ratios
gen ratio_1_5=weekly_var/daily_var
gen log_ratio_1_5=weekly_log_var/daily_log_var
gen ratio_1_10=ten_var/daily_var
gen log_ratio_1_10=ten_log_var/daily_log_var
gen ratio_1_20=twen_var/daily_var
gen log_ratio_1_20=twen_log_var/daily_log_var
gen ratio_1_50=fif_var/daily_var
gen log_ratio_1_50=fif_log_var/daily_log_var

br comnam crisis daily_var daily_log_var weekly_var weekly_log_var ten_var ten_log_var twen_var twen_log_var fif_var fif_log_var ratio_1_5 log_ratio_1_5 ratio_1_10 log_ratio_1_10 ratio_1_20 log_ratio_1_20 ratio_1_50 log_ratio_1_50 if big6 == 1 

br comnam crisis ratio_1_5 log_ratio_1_5 ratio_1_10 log_ratio_1_10 ratio_1_20 log_ratio_1_20 ratio_1_50 log_ratio_1_50 if big6 == 1 

br comnam crisis ratio_1_5 log_ratio_1_5 ratio_1_10 log_ratio_1_10 ratio_1_20 log_ratio_1_20 ratio_1_50 log_ratio_1_50 if big6 == 0

*** we need to fix a few things for these tables to come out correctly 
/* First CIT has two diff permnos 

replace permno = 93150 if comnam == "C I T GROUP INC NEW" 

drop if permno == 13384 | permno == 14558 | permno == 14776 | permno == 14889

br comnam crisis ratio_1_5 log_ratio_1_5 ratio_1_10 log_ratio_1_10 ratio_1_20 log_ratio_1_20 ratio_1_50 log_ratio_1_50 if big6 == 0*/ 

br comnam crisis ratio_1_5 log_ratio_1_5 ratio_1_10 log_ratio_1_10 ratio_1_20 log_ratio_1_20 ratio_1_50 log_ratio_1_50 if permno == 13384 | permno == 14558 | permno == 14776 | permno == 14889
